#include "ocv_service_impl.h"
#include "config_loader.h"

using grpc::Server;

void RunServer(const ConfigMap &config) {
  std::string server_address("0.0.0.0:" + config.at(PORT));
  OcvServiceImpl service(config.at(VIDEO_DEVICE), std::stoi(config.at(JPEG_QUALITY)));

  ServerBuilder builder;
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  builder.RegisterService(&service);

  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  server->Wait();
}

int main(int argc, char** argv) {
  ConfigMap config = ConfigLoader().GetConfig("config.ini");
  // show config
  for (auto& item : config) {
    std::cout << item.first << " " << item.second << std::endl;
  }
  RunServer(config);
  return 0;
}